package cn.itrip.auth.service;

import cn.itrip.auth.exception.DatabaseException;
import cn.itrip.auth.exception.LoginException;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.MD5;
import cn.itrip.common.RedisAPI;
import cn.itrip.mapper.itripUser.ItripUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2020/3/11 21:11
 * @Version: 1.0
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    private ItripUserMapper itripUserMapper;
    @Resource
    private MailService mailService;
    @Resource
    private RedisAPI redisAPI;
    @Resource
    private SmsService smsService;


    @Override
    public void itriptxCreateUser(ItripUser user) throws Exception {

        //1、添加用户
        itripUserMapper.insertItripUser(user);
        //2、生成激活码
        String activationCode = MD5.getMd5(new Date().toString(),32);
        //3、发送邮件
        mailService.sendActivationCode(user.getUserCode(),activationCode);
        //4、激活码存入Redis
        redisAPI.set("activation:"+user.getUserCode(),30*60,activationCode);
    }

    @Override
    public boolean activate(String mail, String code) throws Exception {
        //1、验证激活码
        String value = redisAPI.get("activation:"+mail);
        if(value.equals(code)){
            //根据userCode查找用户
            Map<String,Object> map = new HashMap<>();
            map.put("userCode",mail);
            List<ItripUser> itripUserList =  itripUserMapper.getItripUserListByMap(map);
            if(itripUserList.size()>0){
                ItripUser user = itripUserList.get(0);
                //2、更新用户
                user.setActivated(1);
                //自主注册用户
                user.setUserType(0);
                //记录平台id
                user.setFlatID(user.getId());
                itripUserMapper.updateItripUser(user);
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }

    }

    @Override
    public ItripUser findUserByUserCode(String userCode) throws Exception {
        //根据userCode查找用户
        Map<String,Object> map = new HashMap<>();
        map.put("userCode",userCode);
        List<ItripUser> list =  itripUserMapper.getItripUserListByMap(map);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public void itriptxCreateUserByPhone(ItripUser user) throws Exception {
        //1、创建用户
        itripUserMapper.insertItripUser(user);
        //2、生成验证码
        int code = MD5.getRandomCode();
        //3、发送验证码
        smsService.send(user.getUserCode(),"1",new String[]{String.valueOf(code),"1"});
        //4、添加到Redis
        redisAPI.set("activation:"+user.getUserCode(),5*60,String.valueOf(code));

    }

    @Override
    public boolean validatePhone(String phoneNum, String code) throws Exception {
        //比较验证码
        String key = "activation:"+phoneNum;
        String value = redisAPI.get(key);
        if(null != value && value.equals(code)){
            ItripUser user = this.findUserByUserCode(phoneNum);
            if(null!=user){
                //更新用户状态
                user.setUserType(0);
                user.setFlatID(user.getId());
                user.setActivated(1);
                itripUserMapper.updateItripUser(user);
                return true;
            }

        }

        return false;
    }

    @Override
    public ItripUser checkLogin(String userCode, String password) throws DatabaseException,LoginException {
        Map<String,Object> params = new HashMap<>();
        params.put("userCode",userCode);
        params.put("userPassword",MD5.getMd5(password,32));
        List<ItripUser> users = null;
        try {
            users = itripUserMapper.getItripUserListByMap(params);
        }catch (Exception e){
            e.printStackTrace();
            throw new DatabaseException("数据库异常",e);
        }
        if(EmptyUtils.isEmpty(users)){
            return null;
        }else{
            ItripUser user = users.get(0);
            if(user.getActivated()==0){
                //未激活
                throw new LoginException("账户未激活");
            }else{
                return user;
            }
        }

    }
}
